//
//  ViewController.m
//  AddressBookdemo
//
//  Created by 黄世光 on 15/11/27.
//  Copyright © 2015年 SHG. All rights reserved.
//

#import "ViewController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <ContactsUI/CNContactPickerViewController.h>
#import "UIImage+GIF.h"//动画
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate>
@property(nonatomic,strong)UILabel *telLabel;
@property(nonatomic,strong)UILabel *nameLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(200, 400, 100 ,100)];
    img.image = [UIImage sd_animatedGIFNamed:@"up_loading"];
    [self.view addSubview:img];
    self.telLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, 200, 30)];
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, CGRectGetMaxY(self.telLabel.frame)+10, 200, 30)];
    [self.view addSubview:self.telLabel];
    [self.view addSubview:self.nameLabel];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, CGRectGetMaxY(self.nameLabel.frame)+10, 100, 100)];
    
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
-(void)btnClick{
#if 0
    float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
    if(sysVer >= 9){
        CNContactPickerViewController * vc = [[CNContactPickerViewController alloc] init];
        vc.displayedPropertyKeys = @[CNContactPhoneNumbersKey];//隐藏手机号下面的内容
        vc.delegate = self;
        [self presentViewController:vc animated:YES completion:nil];
        return;
    }
#else//
    ABPeoplePickerNavigationController * vc = [[ABPeoplePickerNavigationController alloc] init];
    vc.displayedProperties = [NSArray arrayWithObject: [NSNumber numberWithInt:kABPersonPhoneProperty]];//隐藏手机号下面的内容
    vc.peoplePickerDelegate = self;
    
    [self presentViewController:vc animated:YES completion:nil];
#endif
}
#if 0
#pragma mark -- ABPeoplePickerNavigationControllerDelegate
#pragma mark -- CNContactPickerDelegate 通讯录
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
    CNContact *contact = contactProperty.contact;
    CNPhoneNumber *phoneNumber = contactProperty.value;
    mobileField.text = phoneNumber.stringValue;
    nameField.text = [NSString stringWithFormat:@"%@%@",contact.familyName?:@"",contact.givenName?:@""];
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    
    
    
    ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    
    CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
    
    //电话号码
    if(index<0){
        index = 0;
    }
    CFStringRef telValue = ABMultiValueCopyValueAtIndex(valuesRef,index);
    
    
    
    //读取firstname
    
    //获取个人名字（可以通过以下两个方法获取名字，第一种是姓、名；第二种是通过全名）。
    
    //第一中方法
    
    //    CFTypeRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    
    //    CFTypeRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    
    //    //姓
    
    //    NSString * nameString = (__bridge NSString *)firstName;
    
    //    //名
    
    //    NSString * lastString = (__bridge NSString *)lastName;
    
    //第二种方法：全名
    
    CFStringRef anFullName = ABRecordCopyCompositeName(person);
    
    __weak typeof(self)wSelf = self;
    [self dismissViewControllerAnimated:YES completion:^{
        
        wSelf.telLabel.text = (__bridge NSString *)telValue;
        
        //        self.nameLabel.text = [NSString stringWithFormat:@"%@%@",nameString,lastString];
        
        wSelf.nameLabel.text = [NSString stringWithFormat:@"%@",anFullName];
        
        
    }];
    
}

#else
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
    [self gainAddBookMessage:person];
}
- (void)gainAddBookMessage:(ABRecordRef)person
{
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFStringRef anFullName = ABRecordCopyCompositeName(person); //联系人姓名
    NSString *name = (__bridge NSString *)(anFullName);
    
    NSMutableArray *phoneArray = [NSMutableArray array];
    
    // 2. 获取所有联系人记录
    CFIndex phoneCount = ABMultiValueGetCount(phones);
    for (NSInteger i = 0; i < phoneCount; i++) {
        
        NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        
        if ([phoneValue hasPrefix:@"+"]) {
            phoneValue = [phoneValue substringFromIndex:3];
        }
        
        phoneValue = [phoneValue stringByReplacingOccurrencesOfString:@"-" withString:@""];
        
        [phoneArray addObject:phoneValue];
        
    }
    
    CFRelease(phones);
    
    if (phoneArray && phoneArray.count == 1) {
        
        self.telLabel.text = name;
        
        //        self.nameLabel.text = [NSString stringWithFormat:@"%@%@",nameString,lastString];
        
        self.nameLabel.text = phoneArray.lastObject;
    }else{
        self.telLabel.text = name;
        
        //        self.nameLabel.text = [NSString stringWithFormat:@"%@%@",nameString,lastString];
        
        self.nameLabel.text = phoneArray.lastObject;
    }
}
#endif



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
